package com.yucquan.app.activity;

import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.exteam.common.base.BaseFragmentActivity;
import com.yucquan.app.AppController;
import com.yucquan.app.R;
import com.yucquan.app.fragment.MessageFragment;
import com.yucquan.app.fragment.TheaterFragment;
import com.yucquan.app.fragment.WoFragment;

/**
 * 功能描述：首页控制器
 *
 * @author yangz
 */
public class HomeFragController extends AppController {

    private TheaterFragment theaterFragment;
    private MessageFragment messageFragment;
    private WoFragment woFragment;
    private Fragment[] fragments;       //三个Fragment数组
    private ImageView[] imageButtons;   //底部三个按钮ImageView的数组
    private TextView[] textViews;       //底部三个按钮TextView的数组
    private int index;                  //点击按钮的标记
    private int currTabIndex;           //当前选中按钮的标记

    public HomeFragController(BaseFragmentActivity act) {
        super(act);
    }

    @Override
    protected void initView() {
        theaterFragment = new TheaterFragment();
        messageFragment = new MessageFragment();
        woFragment = new WoFragment();

        fragments = new Fragment[]{theaterFragment, messageFragment, woFragment};

        imageButtons = new ImageView[3];
        imageButtons[0] = (ImageView) currFragAct.findViewById(R.id.iv_tab1);
        imageButtons[1] = (ImageView) currFragAct.findViewById(R.id.iv_tab2);
        imageButtons[2] = (ImageView) currFragAct.findViewById(R.id.iv_tab3);

        textViews = new TextView[3];
        textViews[0] = (TextView) currFragAct.findViewById(R.id.tv_tab1);
        textViews[1] = (TextView) currFragAct.findViewById(R.id.tv_tab2);
        textViews[2] = (TextView) currFragAct.findViewById(R.id.tv_tab3);

        //初始化底部按钮条的状态
        textViews[0].setTextColor(Color.parseColor("#333333"));
        imageButtons[0].setSelected(true);
        currTabIndex = 0;
        index = 0;

        //初始化当前显示的Fragment页面
        currFragAct.getSupportFragmentManager().beginTransaction()
                .add(R.id.fl_container, theaterFragment)
                .add(R.id.fl_container, messageFragment)
                .add(R.id.fl_container, woFragment)
                .hide(messageFragment).hide(woFragment)
                .show(theaterFragment).commit();
    }

    @Override
    protected void initEventListener() {

    }

    @Override
    public void onViewClick(View v) {
        switch (v.getId()) {
            case R.id.ll_tab1:
                index = 0;
                break;
            case R.id.ll_tab2:
                index = 1;
                break;
            case R.id.ll_tab3:
                index = 2;
                break;
        }

        //更新当前显示的Fragment页面
        if (currTabIndex != index) {
            FragmentTransaction ft = currFragAct.getSupportFragmentManager()
                    .beginTransaction();
            ft.hide(fragments[currTabIndex]);
            if (!fragments[index].isAdded()) {
                ft.add(R.id.fl_container, fragments[index]);
            }
            ft.show(fragments[index]).commit();
        }

        //更新底部按钮条的状态
        imageButtons[currTabIndex].setSelected(false);
        imageButtons[index].setSelected(true);
        textViews[currTabIndex].setTextColor(Color.parseColor("#333333"));
        textViews[index].setTextColor(Color.parseColor("#0088ff"));
        currTabIndex = index;

    }

}
